package com.lotter.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lotter.api.service.ClickRecordService;
import com.lotter.api.service.HousernewsService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.enumobject.ClickTypeEunm;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.StringUtil;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.hoursenew.HouserNews;
import com.lotter.model.user.UserAccount;

@Controller
@RequestMapping(value="/website/news")
public class HousernewsController extends BaseController {
	
	@Autowired
	private HousernewsService housernewsService;
	
	@Autowired
	private UserAccountService userAccountService ;
	
	@Autowired
	private ClickRecordService clickRecordService;

	
	
	
	/**
	 * 保存房间新闻
	 * @param request
	 * @param housernews
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public Map<String,Object> insert(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value ="lrycis",required = true) String lrycis,
			@RequestParam(value ="pics",required = false) String pics) {
		if(StringUtil.isBlank(lrycis))
			return ajaxLotterStage("新闻内容必须传入");
		try {
			int number  =  lrycis.toCharArray().length;
			if(number > 1000 || number < 2){
				return ajaxLotterStage("评论内容在2 到 1000 个字符之内");
			}
			Long uid = StaticDataContext.onLineUid(token);
			housernewsService.insert(uid, lrycis, pics);
		} catch (Exception e) {
			logger.error("保存房间信息失败,请联系管理员");
			return ajaxLotterServer("保存房间信息失败,请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	
	
	/**
	 * 房间新闻顶一下
	 * @param request
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/topnumHouserNews",method = RequestMethod.GET)
	public Map<String,Object> topnumHouserNews(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value ="hid",required = true) Long hid){
		if(hid == null){
			return ajaxLotterStage("参数不正确，请重新请求");
		}
		try {
			HouserNews houserNews = housernewsService.selectByPrimaryKey(hid);
			if(houserNews == null){
				return ajaxLotterStage("没有相关新闻");
			}
			Long uid = StaticDataContext.onLineUid(token);
			Boolean bool = clickRecordService.findRecord(uid, hid,null);
			if(bool){
				return ajaxLotterUser("一个新闻一个用户只能点击一次");
			}
			housernewsService.updateTopORFoot(hid,uid, ClickTypeEunm.TOPCLICK.getCode());
		} catch (LotterCusException e) {
			logger.error("房间信息顶一下出错");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	/**
	 * 房间新闻踩一下
	 * @param request
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/footnumHouserNews",method = RequestMethod.GET)
	public Map<String,Object> footnumHouserNews(@RequestParam(value ="token",required = true) String token,
         @RequestParam(value ="hid",required = true) Long hid){
		if(hid == null){
			return ajaxLotterStage("参数不正确，请重新请求");
		}
		try {
			HouserNews houserNews = housernewsService.selectByPrimaryKey(hid);
			if(houserNews == null){
				return ajaxLotterStage("没有相关新闻");
			}
			Long uid = StaticDataContext.onLineUid(token);
			Boolean bool = clickRecordService.findRecord(uid, hid,null);
			if(bool){
				return ajaxLotterUser("一个新闻一个用户只能点击一次");
			}
			housernewsService.updateTopORFoot(hid,uid, ClickTypeEunm.FOOTCLICK.getCode());
		} catch (LotterCusException e) {
			logger.error("房间信息顶一下出错");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess();
	}
	
	/**
	 * 更新数据
	 * @param request
	 * @param id 新闻id
	 * @param lrycis 内容
	 * @param pics url 地址
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/edit",method = RequestMethod.POST)
	public Map<String,Object> edit(@RequestParam(value ="id",required = true)  Long id,
			@RequestParam(value = "token",required=true) String token,
			@RequestParam(value = "lrycis",required=true) String lrycis,
			@RequestParam(value = "pics",required=false) String pics){
	
		if(StringUtil.isBlank(lrycis))
			return ajaxLotterUser("内容参数有误，请重新请求");
		if(id== null){
			return ajaxLotterStage("内容参数有误，id没有接收到");
		}
		
		try {
			HouserNews houserNews = housernewsService.selectByPrimaryKey(id);
			if(houserNews == null)
				return ajaxLotterStage("ID参数有误，请重新请求");
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			
			if(!userAccount.getId().equals(houserNews.getUid()))
				return ajaxLotterUser("房主只能更新自己的新闻");
			housernewsService.updateHousernews(id, lrycis, pics);
		} catch (LotterCusException e) {
			logger.error("更新房主新闻查询有误,请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess() ;
	}
	
	
	/**
	 * 查询信息
	 * @param id
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/view",method = RequestMethod.GET)
	public Map<String,Object> view(@RequestParam(value ="id",required = true)  Long id,
			@RequestParam(value = "token",required=true) String token){
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			HouserNews houserNews = housernewsService.selectByPrimaryKey(id);
			if(houserNews == null)
				return ajaxLotterStage("ID参数有误，请重新请求");
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			
			if(!userAccount.getId().equals(houserNews.getUid()))
				return ajaxLotterUser("房主只能更新自己的新闻");
			result.put("houserNews", houserNews);
		} catch (LotterCusException e) {
			logger.error("更新房主新闻查询有误,请检查代码");
			return ajaxLotterServer("服务器异常，请联系管理员");
		}
		return ajaxLotterSuccess(result) ;
	}

}
